package com.tttare.haoyigou.manager.controller;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tttare.haoyigou.pojo.TbBrand;
import com.tttare.haoyigou.sellers.service.BrandService;

import entity.PageResult;
import entity.Result;

@Controller
@RequestMapping("/brand")
public class BrandController {

	@Reference
	public BrandService brandService;
	
	@RequestMapping("/findAll")
	@ResponseBody
	public List<TbBrand> findAll(){
		List<TbBrand> list=brandService.findAll();
		return list;
	}
	
	@RequestMapping("/findPage")
	@ResponseBody
	public PageResult<TbBrand> findPage(int page,int size){
		PageResult<TbBrand> pageResult=brandService.findPage(page,size);
		return pageResult;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Result add(@RequestBody TbBrand tbBrand){
		Result result = new Result();
		try {
			brandService.add(tbBrand);
			result.setSuccess(true);
			result.setMessage("添加成功");
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("添加失败");
		}
		return result;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Result update(@RequestBody TbBrand tbBrand){
		Result result = new Result();
		try {
			brandService.update(tbBrand);
			result.setSuccess(true);
			result.setMessage("更新成功");
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage("更新失败");
		}
		return result;
	}
	@RequestMapping("/save")
	@ResponseBody//save方法将update与add写入一个方法中
	public Result save(@RequestBody TbBrand tbBrand){
		Result result = new Result();
		if(tbBrand!=null&&tbBrand.getId()!=null){
			//更新
			result = update(tbBrand);
		}else if(tbBrand!=null&&tbBrand.getId()==null){
			//保存
			result = add(tbBrand);
		}
		return result;
	}
	
	
	@RequestMapping("/search")
	@ResponseBody
	public PageResult<TbBrand> search(@RequestBody TbBrand tbBrand,int page,int size){
		//条件分页,方法重载
		PageResult<TbBrand> pageResult=brandService.findPage(tbBrand,page, size);
		return pageResult;
	}
	
	@RequestMapping("/findOne")
	@ResponseBody
	public TbBrand findOne(Long id){
		TbBrand tbBrand = brandService.findById(id);
		return tbBrand;
	}
	
	@RequestMapping("/selectOptionList")
	@ResponseBody
	public List<Map> selectOptionList(){
		List<Map> list = brandService.selectOptionList();
		return list;
	}
	
	
}
